//
//  XHInformationViewController.m
//  xidian
//
//  Created by 冷山 on 16/5/20.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHInformationViewController.h"
#import "XHInfoDetailViewController.h"

#import "XHInformationCell.h"

#import "Services.h"

#import <SDWebImage/UIImageView+WebCache.h>
#import <DateTools/DateTools.h>
#import <SVPullToRefresh/SVPullToRefresh.h>

static const NSUInteger kPageSize = 20;
static const NSUInteger kPageInit = 0;

@interface XHInformationViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSMutableArray *m_newsArray;

@property (assign, nonatomic) NSUInteger page;
@property (assign, nonatomic) BOOL hasNextPage;
@end

@implementation XHInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [PublicMethod setExtraCellLineHidden:self.ui_tableView];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHInformationCell" bundle:nil]
            forCellReuseIdentifier:XHInformationCellIdentifier];
    
    self.page = kPageInit;
    [self.ui_tableView addInfiniteScrollingWithActionHandler:^{
        if (self.m_newsArray.count>=kPageSize){
            [self loadNewsDataWithPage:self.page+1];
            return;
        }
        [self.ui_tableView.infiniteScrollingView stopAnimating];
    }];
    [self.ui_tableView addPullToRefreshWithActionHandler:^{
        self.page=kPageInit;
        [self loadNewsDataWithPage:self.page];
    }];
    [self.ui_tableView.pullToRefreshView setTitle:NSLocalizedString(@"PullToRefresh_Pulling", nil) forState:SVInfiniteScrollingStateStopped];
    [self.ui_tableView.pullToRefreshView setTitle:NSLocalizedString(@"PullToRefresh_WillLoading", nil) forState:SVInfiniteScrollingStateTriggered];
    [self.ui_tableView.pullToRefreshView setTitle:NSLocalizedString(@"PullToRefresh_Loading", nil) forState:SVInfiniteScrollingStateLoading];
    [self.ui_tableView  reloadData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadNewsDataWithPage:kPageInit];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadNewsDataWithPage:(NSInteger)page{
    [ServiceManager getAllNewsWithPageIndex:page success:^(NSDictionary *data) {
        if (page==kPageInit||self.m_newsArray==nil) {
            self.m_newsArray=[NSMutableArray array];
        }
        NSArray * datas=data[@"result"];
        self.hasNextPage=(datas.count==kPageSize);
        if (datas.count>0) {
            self.page=page;
            [self.m_newsArray addObjectsFromArray:datas];
        }
        [self.ui_tableView  reloadData];
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [self.ui_tableView.pullToRefreshView stopAnimating];
    } failure:^(NSDictionary *data) {
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [self.ui_tableView.pullToRefreshView stopAnimating];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableVie
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows=self.m_newsArray.count;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=nil;
    cell = [self informationCell:indexPath];
    return cell;
}

- (XHInformationCell*)informationCell:(NSIndexPath*)indexPath
{
    XHInformationCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHInformationCellIdentifier];
    NSDictionary *dic=self.m_newsArray[indexPath.row];
    cell.ui_titleLabel.text=dic[@"contentTitle"];
    [cell.ui_imageView sd_setImageWithURL:[NSURL URLWithString:dic[@"contentThumbnail"]] placeholderImage:[UIImage imageNamed:@"image_tupian"]];
    NSDate *date=[NSDate dateWithString:dic[@"recDate"]];
    cell.ui_timeLabel.text=[date formattedDateWithFormat:@"yyyy.MM.dd"];
    return cell;
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=85;
    
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic=self.m_newsArray[indexPath.row];
    XHInfoDetailViewController * viewController = [[XHInfoDetailViewController alloc]init];
    viewController.m_newsId = dic[@"sequenceNBR"];
    viewController.title = @"内容详情";
    viewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:viewController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
